package com.example.source_share.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.source_share.mapper.FileMapper;
import com.example.source_share.mapper.UserMapper;
import com.example.source_share.pojo.Files;
import com.example.source_share.pojo.User;
import com.example.source_share.service.user.account.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

//用于返回信息
@RestController
public class UserController {
    //要用到数据库，写下面这行
    @Autowired
    UserMapper userMapper;
    @GetMapping("/")
    public String mianWindow(){
        return"welcome!";
    }
    @GetMapping("/user/all")
    public List<User> getAll(){ //获取全部用户
        return userMapper.selectList(null);
    }
    @GetMapping("/user/byid/{userid}")
    public User getUserByID(@PathVariable int userid){ //通过ID查找用户信息
        return userMapper.selectById(userid);
    }
    @GetMapping("/user/name/{username}")
    public User getUserByName(@PathVariable String username){ //通过用户名查找用户信息
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }
    @GetMapping("/user/add/{username}/{password}") //该不安全的端口已被关闭
    public String addUser( //添加用户
            @PathVariable String username,
            @PathVariable String password){
        //查看用户是否存在
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User exist=userMapper.selectOne(queryWrapper);
        if(exist!=null){
            return "用户存在";
        }
        if(password.length()<6){
            return "密码不得少于6位";
        }
        PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encodedPassword=passwordEncoder.encode(password);
        User user=new User(username,encodedPassword);
        try {
            userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Success!";
    }
    @Autowired
    private InfoService infoService;
    @Autowired
    private FileMapper fileMapper;
    @PostMapping("/user/delete")
    public Map<String,String> del(@RequestParam String username,@RequestHeader("Authorization") String authorizationHeader) {
        Map<String,String> nowLogin=infoService.getinfo(); //需要超级管理员才能删除用户

        Map<String,String> result= new HashMap<>();
        if(!Objects.equals(nowLogin.get("purview"), "Super Admin")){
            result.put("message","权限不足!");
            return result;
        }
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User exsit=userMapper.selectOne(queryWrapper);
        if(exsit==null){
            result.put("message","用户不存在!");
            return result;
        }
        if(Objects.equals(exsit.getPurview(), "Super Admin")){
            result.put("message","无权删除管理员用户!");
            return result;
        }
        System.out.println("来自"+nowLogin.get("name")+"的删除操作，被删除者为:"+exsit.getUsername());
        userMapper.delete(queryWrapper);
        //还需要把该用户的文件也一起删除
        //获取该用户的文件列表
        QueryWrapper<Files> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("author",username);
        List<Files> filesList=fileMapper.selectList(queryWrapper2);
        for (Files files : filesList) {
            String filepath = files.getFilepath();
            FileSystemUtils.deleteRecursively(new File(filepath)); //一一删除本地文件
        }
        fileMapper.delete(queryWrapper2);
        result.put("message","删除成功！");
        return result;
    }
    @PostMapping("/user/myinfomation")
    public Map<String,String> getMyInformation(@RequestHeader("Authorization") String authorizationHeader) {
        Map<String,String> nowLogin=infoService.getinfo();
        return nowLogin;
    }
    @PostMapping("/user/infomation")
    public Map<String,String> getInformation(@RequestParam String username,
            @RequestHeader("Authorization") String authorizationHeader) {
        Map<String,String> nowLogin=infoService.getinfo();
        Map<String,String> result= new HashMap<>();
        if(!Objects.equals(nowLogin.get("purview"), "Super Admin")){
            result.put("message","权限不足!");
            return result;
        }
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User exist=userMapper.selectOne(queryWrapper);
        if(exist!=null){
            result.put("message","用户不存在!");
            return result;
        }
        return result;
    }
}